﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Properties;
using JBeamLibrary.Types;

namespace DriveBeamEditor.Controls.General
{
    public partial class ImagesMenu : UserControl
    {
        #region Fields

        private readonly List<ImageMenu> _items = new List<ImageMenu>();
        private Padding _itemMargin = new Padding(0);

        public event EventHandler<ImageMenu> ItemSelected;
        public event EventHandler<object> ItemRemoving;
        public event EventHandler ItemRemoved;

        #endregion

        #region Properties

        public List<ImageMenu> Items
        {
            get { return _items; }
        }

        public int ItemCount
        {
            get { return _items.Count; }
            set
            {
                ClearItems();
                for (var i = 0; i < value; i++)
                    AddItem(string.Format("item{0}", i), string.Empty, Resources.Ferrari_64);
            }
        }

        public Padding ItemPadding
        {
            get { return dropZone.Padding; }
            set { dropZone.Padding = value; }
        }

        public Padding ItemMargin
        {
            get { return _itemMargin; }
            set
            {
                _itemMargin = value;
                foreach (var item in _items)
                {
                    item.Padding = value;
                    item.Size = new Size(64 + value.Left + value.Right, 64 + value.Top + value.Bottom);
                    item.Invalidate();
                }
            }
        }

        #endregion

        #region CTORs

        public ImagesMenu()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void dropZone_ControlRemoved(object sender, ControlEventArgs e)
        {
            if (ItemRemoved != null)
                ItemRemoved(this, new EventArgs());
        }

        #endregion

        #region Methods

        public ImageMenu AddItem(TaggedString item, Image image)
        {
            return AddItem(item.String, item.Tag, image);
        }

        public ImageMenu AddItem(string text, object tag, Image image)
        {
            var item = new ImageMenu
            {
                Text = text,
                Tag = tag,
                Dock = DockStyle.Left,
                Image = image,
                Padding = ItemMargin,
                Size = new Size(64 + ItemMargin.Left + ItemMargin.Right, 64 + ItemMargin.Top + ItemMargin.Bottom)
            };
            item.MouseClick += (s, e) => { if (e.Button == MouseButtons.Left) SelectItem(item); };
            item.Remove += (s, e) => RemoveItem(item);
            _items.Add(item);
            dropZone.Controls.Add(item);

            return item;
        }

        public void RemoveItem(ImageMenu item)
        {
            if (_items.Contains(item))
            {
                var tag = item.Tag;

                if (ItemRemoving != null)
                    ItemRemoving(this, tag);

                _items.Remove(item);
                dropZone.Controls.Remove(item);

                //if (ItemRemoved != null)
                //    ItemRemoved(this, tag);
            }
        }

        public void RemoveItemByLabel(string label)
        {
            if (string.IsNullOrEmpty(label)) return;
            var vItem = _items.FirstOrDefault(item => item.Text.Equals(label));
            if (vItem != null) RemoveItem(vItem);
        }

        public void RemoveItemByTag(object tag)
        {
            var vItem = _items.FirstOrDefault(item => item.Tag == tag);
            if (vItem != null) RemoveItem(vItem);
        }

        public void ClearItems()
        {
            _items.Clear();
            dropZone.Controls.Clear();
        }

        public void SelectItem(ImageMenu item)
        {
            foreach (var it in _items)
            {
                it.IsSelected = it == item;

                if (it == item && ItemSelected != null)
                    ItemSelected(this, item);
            }
        }

        public void SelectItemAt(int index)
        {
            if (ItemCount == 0) return;

            var item = Items[0];
            item.IsSelected = true;

            if (ItemSelected != null)
                ItemSelected(this, item);
        }

        #endregion
    }
}
